package com.traffic.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Encoder;

/**
 * 
 * 公用工具类
 * */
public class PubFun {

    /**
     * 生成UUID
     * 
     * @return String
     * */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.substring(0, 8) + uuid.substring(9, 13) + uuid.substring(14, 18) + uuid.substring(19, 23)
                + uuid.substring(24);
    }

    public static boolean isUrl(String url) {
        boolean ret = false;
        Pattern p = Pattern.compile("http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*");
        Matcher m = p.matcher(url);
        ret = m.matches();
        ret = true;
        return ret;
    }

    public static boolean isDate(String dateString) {
        boolean ret = true;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            sf.parse(dateString);
        } catch (java.text.ParseException e) {
            ret = false;
        }

        return ret;

    }

    public static boolean isMobile(String mobile) {
        boolean ret = false;

        if (StringUtil.isNull(mobile))
            return false;
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobile);
        ret = m.matches();

        return ret;
    }

    public static boolean isInt(String str) {
        boolean ret = false;

        Pattern p = Pattern.compile("^\\d*[1-9]\\d*$");
        Matcher m = p.matcher(str);
        ret = m.matches();

        return ret;
    }

    public static String getRandom(int num) {
        StringBuffer sbf = new StringBuffer("");
        for (int i = 0; i < num; i++) {
            sbf.append(new Random().nextInt(9));
        }
        return sbf.toString();
    }

    public static String arrayEnCode(String[][] arr) {
        StringBuilder sb = new StringBuilder();
        if (arr != null && arr.length > 0) {
            int len = arr.length;
            for (int i = 0; i < len; i++) {
                String[] ar = arr[i];
                for (int j = 0; j < ar.length; j++) {
                    sb.append(ar[j]);
                    if (j != ar.length - 1) {
                        sb.append("\271A");
                    }
                }
                if (i != len - 1) {
                    sb.append("\271A\271A");
                }
            }
        }
        return sb.toString();
    }

    public static String[][] arrayDeCode(String str) {
        String[] arr = str.split("\271A\271A");
        int len = arr.length;
        String[][] arrRet = new String[len][];
        for (int i = 0; i < len; i++) {
            arrRet[i] = arr[i].split("\271A");
        }
        return arrRet;
    }

    public static String arrayJoin(String[] arr, String expr) {
        StringBuilder sb = new StringBuilder();
        if (arr != null && arr.length > 0) {
            int len = arr.length;
            for (int i = 0; i < len; i++) {
                sb.append(arr[i]).append(expr);
            }
            len = sb.toString().length();
            sb.delete(len - expr.length(), len);
        }
        return sb.toString();
    }

    public static String produceDegistCode() {
        String seq = PubFun.getUUID();
        try {
            MessageDigest md5Code = MessageDigest.getInstance("md5");
            byte[] bTmp = md5Code.digest(seq.getBytes());
            // 采用base64算法将字节数组转换成字符串
            BASE64Encoder base64 = new BASE64Encoder();
            seq = base64.encode(bTmp).substring(0, 20);
            // seq="HvB+1PkB+tbqxeBF46A/";
            if (seq.indexOf("+") != -1) {
                seq = seq.replace("+", "b");
            }
            if (seq.indexOf("/") != -1) {
                seq = seq.replace("/", "a");
            }
        } catch (NoSuchAlgorithmException e) {
        }
        return seq;
    }

    public static void main(String[] args) {
        System.out.println(produceDegistCode());
    }
}
